package cc.yaomm.common.basic.file;

import cc.yaomm.common.basic.base.SuperEnum;
import cc.yaomm.common.basic.file.properties.AliOSSProperties;
import cc.yaomm.common.basic.file.properties.FileProperties;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.aliyun.oss.model.DeleteObjectsResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.ByteArrayInputStream;
import java.util.List;

/**
 * Created by XQL on 2020/5/15.
 */
@Slf4j
public class AliOSSService implements FileService {

    @Autowired
    FileProperties fileProperties;

    @Autowired
    AliOSSProperties aliOSSProperties;

    @Override
    public FileProperties getFileProperties() {
        return fileProperties;
    }

    @Override
    public String upload(String fileName, byte[] fileData, SuperEnum<?, String> directory, boolean isRewrite) {
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(aliOSSProperties.getEndpoint(),
                aliOSSProperties.getAppKey(), aliOSSProperties.getAppSecret());

        //获取文件名
        String filePath = this.getFilePath(directory.getValue(), fileName, isRewrite);

        // 上传Byte数组。
        ossClient.putObject(aliOSSProperties.getBucket(), filePath, new ByteArrayInputStream(fileData));

        // 关闭OSSClient。
        ossClient.shutdown();

        return filePath;
    }

    /**
     * <p>@Description: 删除文件</p>
     * <p>@Author: QiLin.Xing </p>
     * <p>@Date: 2020/5/17 15:56 </p>
     * <p>@Version: 1.0
     *
     * @param filePath 文件路径
     * @return
     */
    public boolean delete(String filePath) {
        if (StringUtils.isEmpty(filePath)) {
            log.warn("删除文件失败，文件路径为空");
            return false;
        }

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(aliOSSProperties.getEndpoint(),
                aliOSSProperties.getAppKey(), aliOSSProperties.getAppSecret());

        // 删除文件。如需删除文件夹，请将ObjectName设置为对应的文件夹名称。如果文件夹非空，则需要将文件夹下的所有object删除后才能删除该文件夹。
        ossClient.deleteObject(aliOSSProperties.getBucket(), filePath);

        // 关闭OSSClient。
        ossClient.shutdown();

        return false;
    }

    /**
     * <p>@Description: 批量删除文件</p>
     * <p>@Author: QiLin.Xing </p>
     * <p>@Date: 2020/5/17 15:51 </p>
     * <p>@Version: 1.0
     *
     * @param filePathList 文件路径集合
     * @return
     */
    public boolean delete(List<String> filePathList) {
        if (CollectionUtils.isEmpty(filePathList)) {
            log.warn("批量删除文件失败，文件路径为空");
            return false;
        }

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(aliOSSProperties.getEndpoint(),
                aliOSSProperties.getAppKey(), aliOSSProperties.getAppSecret());

        DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(
                new DeleteObjectsRequest(aliOSSProperties.getBucket()).withKeys(filePathList));
        List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();

        // 关闭OSSClient。
        ossClient.shutdown();

        return deletedObjects.size() > 0;
    }

}
